<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/12 0012
 * Time: 14:04
 */

namespace backend\controllers;


use backend\components\BaseComponentController;
use common\components\ActionsComponents\ListViewAction;
use common\models\Admin\Admin;

class RegionManagerController extends BaseComponentController
{
    public function actions()
    {
        return [
            "index" => [
                "class" => ListViewAction::class,
                "model" => Admin::class,
                "condition" => [
                    "type" => Admin::TYPE_REGION_MANAGER
                ],
                "with" => "regionManagerInfo"
            ]
        ]; // TODO: Change the autogenerated stub
    }

    public function actionItem()
    {
        if ( ($id = $this->get("id")) ) {
            $user = Admin::findOne([
                "id" => $id,
                "type" => Admin::TYPE_REGION_MANAGER
            ]);
        } else {
            $user = [];
        }
        return $this->render("item", [
            "ret" => $user
        ]);
    }

    public function actionUpdateStatus()
    {
        $user = Admin::findOne([
            "id" => $this->requestAll("id"),
            "type" => Admin::TYPE_REGION_MANAGER
        ]);
        if ( !$user ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "未知的用户");
        }

        return $user->changeStatus($this->requestAll("status")) ? $this->jsonResponse(self::RESPONSE_SUCCESS) : $this->jsonResponse(self::RESPONSE_FAILURE);
    }

    public function actionSet()
    {
        if ( Admin::setUserAccount($this->post(), Admin::TYPE_REGION_MANAGER, $this->post("id")) ) {
            return $this->jsonResponse(self::RESPONSE_SUCCESS);
        }
        return $this->jsonResponse(self::RESPONSE_FAILURE, Admin::error());
    }
}